﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using QLKCB.Controller;
using QLKCB.Common;

namespace QLKCB
{
    public partial class Form_QLKhamChuaBenh : Form
    {

        cBenhNhan benhNhanController = new cBenhNhan();
        Utils utils = new Utils();
        private string maBN;
        private int countLoad;

        public Form_QLKhamChuaBenh()
        {
            InitializeComponent();
        }

        private void Form_QLKhamChuaBenh_Load(object sender, EventArgs e)
        {

            countLoad = 0;
            showDanhSach("");

        }

        private void showDanhSach(string strTimKiem)
        {

            DataTable dtDanhSach;
            dgvDSBN.Rows.Clear();
            if (strTimKiem.Equals(""))
            {
                dtDanhSach = benhNhanController.getDanhSach();
            }
            else
            {
                dtDanhSach = benhNhanController.timKiemBenhNhan(strTimKiem);
            }
            int rowSize = dtDanhSach.Rows.Count;
            int colSize = dtDanhSach.Columns.Count;

            Hashtable mapDoiTuong = utils.getDoiTuong();

            for (int i = 0; i < rowSize; i++)
            {
                string[] arrData = new string[5];
                int indexData = 0;
                for (int j = 0; j < colSize; j++)
                {
                    if (j == 0 || j == 1 || j == 2 || j == 4)
                    {
                        if (j == 2)
                        {
                            arrData[indexData] = mapDoiTuong[dtDanhSach.Rows[i][j].ToString()].ToString();
                        }
                        else
                        {
                            arrData[indexData] = dtDanhSach.Rows[i][j].ToString();
                        }                        
                        indexData++;
                    }
                    else if (j == 5)
                    {
                        string ngaySinh = dtDanhSach.Rows[i][j].ToString();
                        char[] sep = { ' ' };
                        arrData[indexData] = ngaySinh.Split(sep)[0] ;
                    }
                }

                dgvDSBN.Rows.Add(arrData);
            }

        }

        private void btnTimKiemBN_Click(object sender, EventArgs e)
        {
            string strTimKiem = txtTimKiemBN.Text;
            showDanhSach(strTimKiem);
        }

        private void txtTimKiemBN_Click(object sender, EventArgs e)
        {
            txtTimKiemBN.Text = "";
        }

        private void txtTimKiemBN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strTimKiem = txtTimKiemBN.Text;
                showDanhSach(strTimKiem);
            }
        }

        private void btnThemBN_Click(object sender, EventArgs e)
        {
            Form_ThemBenhNhan themBenhNhan = new Form_ThemBenhNhan();
            themBenhNhan.Show();
            this.Hide();
        }

        private void dgvDSBN_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            string maBN = dgvDSBN.Rows[e.RowIndex].Cells[0].Value.ToString();
            Form_ThemBenhNhan themBenhNhan = new Form_ThemBenhNhan(maBN);
            themBenhNhan.Show();
            this.Hide();

        }

        private void Form_QLKhamChuaBenh_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void btnDKKB_Click(object sender, EventArgs e)
        {
            if (maBN == null)
            {
                if (countLoad == 0)
                {
                    maBN = dgvDSBN.Rows[0].Cells[0].Value.ToString();
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn bệnh nhân cần đăng ký khám chữa bệnh");
                }
            }
            
            if(maBN != null)
            {
                Form_DangKyKhamBenh form = new Form_DangKyKhamBenh(maBN);
                form.Show();
                this.Hide();
            }
        }

        private void dgvDSBN_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int sizeDataTable = dgvDSBN.Rows.Count;
            countLoad = 1;
            if (e.RowIndex >= 0 && e.RowIndex < sizeDataTable - 1)
            {
                maBN = dgvDSBN.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
            else
            {
                maBN = null;
            }
        }

        private void btnHSBA_Click(object sender, EventArgs e)
        {
            if (maBN == null)
            {
                if (countLoad == 0)
                {
                    maBN = dgvDSBN.Rows[0].Cells[0].Value.ToString();
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn bệnh nhân cần làm hồ sơ bệnh án");
                }
            }

            if (maBN != null)
            {
                Form_HoSoBenhAn form = new Form_HoSoBenhAn(maBN);
                form.Show();
                this.Hide();
            }
        }

    }
}
